package com.huyi.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.huyi.common.result.APIResponse;
import com.huyi.web.entity.User;
import com.huyi.web.service.UserService;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/hy/user")
public class UserController {
   
    @Autowired
    private UserService userService;

    /**
     * redis发布式锁
     * 
     * @param request
     * @param userId
     * @return
     * @throws Exception
     */
	@ApiOperation(value = "查询用户信息demo")
	@RequestMapping(value = "/getUserInfoByRedis/{loginId}", method = RequestMethod.GET)
    public JSONObject getUserInfoByRedis(HttpServletRequest request, @PathVariable("loginId") String loginId) throws Exception {
        User user1 = new User();
        user1.setLoginId(loginId);
        User user = userService.findUserByLoginId(user1);
        log.info(JSON.toJSONString(user));
        return APIResponse.returnSuccess(JSON.toJSONString(user));
    }
	
	/**
	 * zookeeper分布式锁
	 * 
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "查询用户信息demo")
	@RequestMapping(value = "/getUserInfoByCurator/{loginId}", method = RequestMethod.GET)
    public JSONObject getUserInfoByCurator(@PathVariable("loginId") String loginId) throws Exception {
        User user1 = new User();
        user1.setLoginId(loginId);
        User user = userService.findUserByLoginId1(user1);
        return APIResponse.returnSuccess(JSON.toJSONString(user));
    }
	
	/**
	 * mybatis一堆多 嵌套查询
	 * 
	 * @param request
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "mybatis一堆多 嵌套查询demo")
	@GetMapping(value = "/queryUserInfoByUserId/{userId}")
    public JSONObject queryUserInfoByUserId(HttpServletRequest request, @PathVariable("userId") String userId) throws Exception {
		List<User> user = userService.queryUserInfoByUserId(userId);
        log.info(JSON.toJSONString(user));
        return APIResponse.returnSuccess(JSON.toJSONString(user));
    }
	
	/**
	 * mybatis一plus分页查询
	 * 
	 * @param request
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "mybatis一plus分页查询")
	@GetMapping(value = "/queryAllUserList")
    public JSONObject queryAllUserList(HttpServletRequest request) throws Exception {
		// 开启分页
		PageHelper.startPage(1, 5);
		List<User> list = userService.queryAllUserList();
		// 获取总条数
		// int total = PageHelper.getTotal();
		// 获取总条数，并释放资源
		// int total = PageHelper.freeTotal();
        return APIResponse.returnDataTable(list);
    }

}
